<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements JWTSubject
{
    use HasFactory, Notifiable, Filterable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'role_id',
        'password',
    ];



    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    /**
     * 角色
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function role()
    {
        return $this->belongsTo(Role::class);
    }


    /**
     * 获取权限名称
     * @return array
     */
    public function getPermissionListAttribute()
    {
        $role = $this->role;
        if (!$role) {
            return [];
        }
        return $role->permissions()->pluck('code')->toArray();
    }



    /**
     * 获取选择的目录
     * @param $menus
     * @param $roleMenu
     * @return array
     */
    public function handleCheck($menus, $roleMenu)
    {
        $data = [];
        foreach ($menus as $menu) {
            $menuChecked = in_array($menu->id, $roleMenu);
            $map = [
                'checked' => false,
                'id' => $menu->id,
                'name' => $menu->name,
                'key' => $menu->key,
                'icon' => $menu->icon,
                'link' => $menu->link,
                'parent_id' => $menu->parent_id,
            ];
            foreach ($menu->children as $key => $child) {
                $checked = in_array($child->id, $roleMenu);
                $childrenMap = [
                    'id' => $child->id,
                    'name' => $child->name,
                    'key' => $child->key,
                    'icon' => $child->icon,
                    'link' => $child->link,
                    'parent_id' => $child->parent_id,
                    'children' => []
                ];
                foreach ($child->children as $k => $c) {
                    $cChecked = in_array($c->id, $roleMenu);
                    $childrenMap['children'][] = [
                        'id' => $c->id,
                        'checked' => $cChecked,
                        'name' => $c->name,
                        'key' => $c->key,
                        'icon' => $c->icon,
                        'link' => $c->link,
                        'parent_id' => $c->parent_id
                    ];
                    if ($cChecked && !$checked) {
                        $checked = true;
                        $menuChecked = true;
                    }
                }
                $childrenMap['checked'] = $checked;
                if ($checked) {
                    $map['children'][] = $childrenMap;
                }
            }
            $map['checked'] = $menuChecked;
            if ($menuChecked) {
                $data[] = $map;
            }
        }
        return $data;
    }
}
